﻿using System;
using TogNet.Configuration;

namespace TogNet.BusinessObjects
{
    public class ConnectionToggler
    {
        const string NetShCommand = "netsh interface set interface";

        public static void ToggleState(NetworkAdapter adapter)
        {
            string command = string.Format("{0} \"{1}\"", NetShCommand, adapter.Name);
            string result = adapter.IsAlreadyEnabled ? DosShell.ExecuteCommand( string.Format("{0} disabled", command) ) 
                                              : DosShell.ExecuteCommand( string.Format("{0} enabled", command) );

            if (!string.IsNullOrEmpty(result.Trim()))
                throw new Exception(result);

            adapter.IsAlreadyEnabled = !adapter.IsAlreadyEnabled;
        }
    }
}
